Skip to content

Add comprehensive unit tests for lib package (90.3% coverage)#274

Draft
Copilot wants to merge 2 commits intomasterfrom
copilot/add-unit-tests-for-lib-package
Draft

Add comprehensive unit tests for lib package (90.3% coverage)#274
Copilot wants to merge 2 commits intomasterfrom
copilot/add-unit-tests-for-lib-package

Conversation

Copy link
Copy Markdown

Copilot AI commented Nov 14, 2025

The lib package had no test coverage. This PR adds comprehensive unit tests achieving 90.3% coverage using only Go's standard testing package.

Test Files Added

  • error_test.go - Error constant validation
  • lib_test.go - Constants, types, and registry tests
  • common_test.go - HTTP utilities and JSON unmarshaling (with httptest mock servers)
  • config_test.go - Config creator registration and unmarshaling
  • converter_test.go - Converter registration
  • entry_test.go - IP prefix processing, marshaling, and set operations
  • container_test.go - Container operations, merging, and lookup
  • instance_test.go - Instance lifecycle and config loading

Coverage Breakdown

Key functions tested:

  • Entry.processPrefix() - All IP types: net.IP, *net.IPNet, netip.Addr, netip.Prefix, strings with CIDR notation
  • Container.Add() - Entry merging with IgnoreIPv4/IgnoreIPv6 options
  • Container.Remove() - Both CaseRemovePrefix and CaseRemoveEntry modes
  • Container.Lookup() - IPv4/IPv6 address and CIDR lookups with search lists
  • Instance.InitConfig() - File and HTTP(S) URL loading with hujson support

Example Test Pattern

func TestEntry_AddPrefix(t *testing.T) {
    tests := []struct {
        name    string
        cidr    any
        wantErr bool
    }{
        {"IPv4 CIDR", "192.168.1.0/24", false},
        {"IPv6 CIDR", "2001:db8::/32", false},
        {"net.IP", net.ParseIP("192.168.1.1"), false},
        {"netip.Prefix", netip.MustParsePrefix("10.0.0.0/8"), false},
    }
    // ...
}

212 tests, zero dependencies, zero failures.

Original prompt

Please write comprehensive unit tests for lib package and make sure the coverage rate is above 90%. Try not to use third-party Golang dependencies if possible.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 14, 2025
Co-authored-by: Loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
Copilot AI changed the title [WIP] Add comprehensive unit tests for lib package Add comprehensive unit tests for lib package (90.3% coverage) Nov 14, 2025
Copilot AI requested a review from Loyalsoldier November 14, 2025 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants